package com.yuan.util.string;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Explain:     []
 * Date:        [2017/5/16 18:44]
 * Coder:       [YouYuan]
 * Version:     [1.0]
 */
public class UrlUtil {
    public static void main(String[] args) {
        String a = "http://www.a.com/e/f/g/h.html";
        String b = "http://www.b.com.cn/e/f/g/h.html?ttt=123";
        String c = "http://c.c.com.jp/e/f/g/h.html";
        String d = "d.com/e/f/g/h.html";
        System.out.println("getPath:" + getPath(a));
        System.out.println(getPath(b));
        System.out.println(getPath(c));
        System.out.println(getPath(d));
        System.out.println("getUrlFileName:" + getUrlFileName(b));
        System.out.println("getDomain:" + getDomain(b));
        System.out.println("getDomainRemoveSLD:" + getDomainRemoveSLD(b));
        System.out.println("matchAllUrl:");
        String html = "为时发的" + a + "3223wer飞 " + b + "rr人人r" + c;
        System.out.println("html:" + html);
//        String html = "url(\"http://wwwa.b.c.ruanyifeng.com.cn/images_pub/pub_12.jpg?data=wer&f=5\");\"";
        Matcher matcher = matchAllUrl(html);
        while (matcher.find()) {
            System.out.println(matcher.group());
        }
    }

    public static String getPath(String url) {
        if (url == null) {
            return null;
        }
        String regex = "(http\\w*://){0,1}(\\w+\\.)+\\w+";
        String path = url.replaceAll(regex, "");
        int index = path.lastIndexOf("/");
        if (index != -1) {
            path = path.substring(0, index + 1);
        }
        return path;
    }

    public static String getFilePath(String url) {
        if (url == null) {
            return null;
        }
        String regex = "(http\\w*://){0,1}(\\w+\\.)+\\w+/";
        return url.replaceAll(regex, "");
    }

    public static String getUrlFileName(String url) {
        if (url == null) {
            return null;
        }
        int index = url.lastIndexOf("/");
        if (index != -1) {
            url = url.substring(index + 1);
        }
        index = url.indexOf("?");
        if (index != -1) {
            url = url.substring(0, index);
        }
        return url;
    }

    public static String getUrlFileNameNoSuffix(String url) {
        String urlFileName = getUrlFileName(url);
        if (urlFileName != null) {
            int i = url.indexOf(".");
            if (i != -1) {
                urlFileName = url.substring(0, i);
            }
        }

        return urlFileName;
    }

    public static String getDomain(String url) {
        if (url == null) {
            return null;
        }
        int index = url.indexOf("://");
        if (index != -1) {
            url = url.substring(index + 3, url.length());
        }
        index = url.indexOf("/");
        if (index != -1) {
            url = url.substring(0, index);
        }
        return url;
    }

    /**
     * 获取url中的域名,去除二级域名
     *
     * @param url
     * @return
     */
    public static String getDomainRemoveSLD(String url) {
        if (url == null) {
            return null;
        }
        int index = url.indexOf("://");
        if (index != -1) {
            url = url.substring(index + 3, url.length());
        }
        index = url.indexOf("/");
        if (index != -1) {
            url = url.substring(0, index);
        }
        System.out.println(url.matches(".+\\..+\\..+"));
        if (url.matches(".+\\..+\\..+")) {
            int i = url.indexOf(".");
            url = url.substring(i + 1, url.length());
        }
        return url;
    }

    /**
     * 匹配文本中所有的url链接地址
     *
     * @param html
     * @return
     */
    public static Matcher matchAllUrl(String html) {
//        Pattern pattern = Pattern.compile("(http://|ftp://|https://)?([\\w-\\d]+\\.)+[^\u4e00-\u9fa5\\s]*(\\.(com|net|org|cn|me|tw|fr|cc|jp))+((\\?|#|/)[^\u4e00-\u9fa5\\s]*)*");//支持匹配中文
        Pattern pattern = Pattern.compile("(http://|ftp://|https://)?([\\w-\\d]+\\.)+[^\u4e00-\u9fa5\\s\\(\\)]*(\\.(com|net|org|cn|me|tw|fr|cc|jp))+((\\?|/)[\u4e00-\u9fa5\\d\\w=@&\\.]*)*");
        Matcher matcher = pattern.matcher(html);
        return matcher;
    }
}
